﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;

namespace YJKTools
{
	public partial class AutoSaveForm : System.Windows.Forms.Form
	{
		public AutoSaveForm()
		{
			this.InitializeComponent();
			this.tbInterval.Text = "1";
		}

		public bool AutoSave
		{
			get
			{
				return this.chkAutoSave.Checked;
			}
		}

		public double Interval
		{
			get
			{
				return Convert.ToDouble(this.tbInterval.Text) * 60.0 * 1000.0;
			}
		}

		public int FileCount
		{
			get
			{
				return Convert.ToInt32(this.numericUpDownSize.Value.ToString());
			}
		}

		private void AutoSaveForm_Load(object sender, EventArgs e)
		{
			string fileName = AssistFunc.GetAppRootPath() + "\\Data\\AutoSaveSettingFile.xml";
			bool @checked = false;
			double num = 30.0;
			int value = 5;
			if (!AutoSaveSettingFileReadWriteKit.ReadMappingFile(fileName, ref @checked, ref num, ref value))
			{
				@checked = false;
				num = 30.0;
				value = 5;
			}
			num = (double)((int)num);
			this.chkAutoSave.Checked = @checked;
			this.tbInterval.Text = num.ToString();
			this.numericUpDownSize.Value = value;
			if (this.chkAutoSave.Checked)
			{
				this.tbInterval.Enabled = true;
				this.numericUpDownSize.Enabled = true;
				return;
			}
			this.tbInterval.Enabled = false;
			this.numericUpDownSize.Enabled = false;
		}

		private void AutoSaveForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (((Form)sender).DialogResult == DialogResult.OK)
			{
				string fileName = AssistFunc.GetAppRootPath() + "\\Data\\AutoSaveSettingFile.xml";
				bool @checked = this.chkAutoSave.Checked;
				double interval = Convert.ToDouble(this.tbInterval.Text);
				int fileCount = Convert.ToInt32(this.numericUpDownSize.Value.ToString());
				AutoSaveSettingFileReadWriteKit.WriteMappingFile(fileName, @checked, interval, fileCount);
			}
		}

		private void tbInterval_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				int num = int.Parse(this.tbInterval.Text);
				if (this.Lessthan_Or_Equal((double)num, 0.0))
				{
					MessageBox.Show("间隔时间必须大于零.", "盈建科提示");
					e.Cancel = true;
				}
			}
			catch (FormatException)
			{
				MessageBox.Show("间隔时间必须为正整数.", "盈建科提示");
				e.Cancel = true;
			}
		}

		private void chkAutoSave_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkAutoSave.Checked)
			{
				this.tbInterval.Enabled = true;
				this.numericUpDownSize.Enabled = true;
				return;
			}
			this.tbInterval.Enabled = false;
			this.numericUpDownSize.Enabled = false;
		}

		private void numericUpDownSize_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				int num = int.Parse(this.numericUpDownSize.Value.ToString());
				if (this.Lessthan_Or_Equal((double)num, 0.0))
				{
					MessageBox.Show("备份文件数量上限必须大于零.", "盈建科提示");
					e.Cancel = true;
				}
			}
			catch (FormatException)
			{
				MessageBox.Show("备份文件数量上限必须为正整数.", "盈建科提示");
				e.Cancel = true;
			}
		}

		private bool LessThan(double val1, double val2)
		{
			return val1 - val2 < -1E-09;
		}

		private bool GreaterThan(double val1, double val2)
		{
			return val1 - val2 > 1E-09;
		}

		private bool IsEqual(double val1, double val2)
		{
			return !this.LessThan(val1, val2) && !this.LessThan(val2, val1);
		}

		private bool Lessthan_Or_Equal(double val1, double val2)
		{
			return !this.GreaterThan(val1, val2);
		}

		private bool Greaterthan_Or_Equal(double val1, double val2)
		{
			return !this.LessThan(val1, val2);
		}

		private const double _epsDouble = 1E-09;
	}
}
